package com.ruoyi.common.utils;

import com.ruoyi.common.core.domain.entity.SysDictData;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@SuppressWarnings("all")
public class NumRangeUtils {
    /**
     * 开闭区间正则表达式
     */
    private static final Pattern NUM_RANGE_PATTERN = Pattern.compile("[\\[|\\(]\\s?\\d+\\s?,\\s?\\d+\\s?[\\)|\\]]");

    /**
     * 左半区间正则表达式
     */
    private static final Pattern LEFT_NUM_RANGE_PATTERN = Pattern.compile("[\\[|\\(]\\s?\\d+\\s?,\\s?[\\)|\\]]");

    /**
     * 右半区间正则表达式
     */
    private static final Pattern RIGHT_NUM_RANGE_PATTERN = Pattern.compile("[\\[|\\(],\\s?\\d+\\s?[\\)|\\]]");


    /**
     * 判断数值是否在区间范围内
     * @param number 数值
     * @param numRange 开闭区间
     * @return boolean
     */
    public static boolean inNumRange(BigDecimal number, String numRange) {
        try {
            Objects.requireNonNull(numRange);
            String[] pairs = numRange.split(",");
            // 获取开闭区间的最小值和最大值
            List<String> rangeNums = Arrays.stream(pairs).map(str -> str.replaceAll("[(|)|\\[|\\]]", "").trim()).collect(Collectors.toList());
            BigDecimal minValue = "".equals(rangeNums.get(0)) ? null : new BigDecimal(rangeNums.get(0));
            BigDecimal maxValue = "".equals(rangeNums.get(1)) ? null : new BigDecimal(rangeNums.get(1));

            // 判定数值是否大于最小值
            boolean minMatched = (minValue == null) || (pairs[0].startsWith("[") ? number.compareTo(minValue) >= 0 : number.compareTo(minValue) > 0);
            // 判定数值是否小于最大值
            boolean maxMatched = (maxValue == null) || (pairs[1].endsWith("]") ? number.compareTo(maxValue) <= 0 : number.compareTo(maxValue) < 0);
            return minMatched && maxMatched;
        } catch (Exception e) {
            System.out.println("设置区间异常");
            return  false;
        }
    }
    /**
     * 判断是否为有效的数字区间范围
     * @param numRange 数字区间
     * @return boolean
     */
    public static boolean isValidNumRange(String numRange) {
        return NUM_RANGE_PATTERN.matcher(numRange).matches()
                || LEFT_NUM_RANGE_PATTERN.matcher(numRange).matches()
                || RIGHT_NUM_RANGE_PATTERN.matcher(numRange).matches();
    }


    /**
     * 每日福利区间
     * @param number 数值
     * @return boolean
     */
    public static Integer inNumRangeFrings(BigDecimal number, List<SysDictData> proportionOfPoints) {
        try {
            for (int i = 0; i < proportionOfPoints.size(); i++) {
                String dictlable = proportionOfPoints.get(i).getDictLabel();
                if (number.compareTo(new BigDecimal(dictlable)) <= 0) {
                    if (i == 0) {
                        return i;
                    } else{
                        return i - 1;
                    }
                }
            }
            return proportionOfPoints.size()-1;
        } catch (Exception e) {
            System.out.println("获取福利区间异常");
            return 0;
        }
    }

    /**
     * 随机获取价格区间价格
     * @param priceRange 开闭区间
     * @return boolean
     */
    public static Double randonPrice(String priceRange) {
        Objects.requireNonNull(priceRange);
        String[] pairs = priceRange.split(",");
        // 获取开闭区间的最小值和最大值
        List<String> rangeNums = Arrays.stream(pairs).map(str -> str.replaceAll("[(|)|\\[|\\]]", "").trim()).collect(Collectors.toList());
        Double minValue = "".equals(rangeNums.get(0)) ? null : Double.parseDouble(rangeNums.get(0));
        Double maxValue = "".equals(rangeNums.get(1)) ? null : Double.parseDouble(rangeNums.get(1));

        minValue = minValue == null ? 0.01 : (pairs[0].startsWith("[") ? minValue : Arith.add(minValue,0.01));
        maxValue = maxValue == null ? 500d : (pairs[1].endsWith("]") ? Arith.add(maxValue,0.01) : maxValue);
        minValue = Arith.mul(minValue,100d);
        maxValue = Arith.mul(maxValue,100d);

        int min  =minValue.intValue();
        int max = maxValue.intValue();
        Integer result = RandomUtils.nextInt(min,max);
        return Arith.div(Double.parseDouble(result.toString()),100d,2);
    }
}
